package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjBanner;
import com.moji.entity.MjBasicInfo;
import com.moji.entity.MjHomeProduct;
import com.moji.entity.vo.CommonBasicDTO;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjBasicInfoMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjBasicInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 网站底部基本信息表(MjBasicInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:35:51
 */
@Service("mjBasicInfoService")
public class MjBasicInfoServiceImpl extends ServiceImpl<MjBasicInfoMapper, MjBasicInfo> implements MjBasicInfoService {
    @Resource
    private MjBasicInfoMapper mjBasicInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjBasicInfo queryById(Long id) {
        return this.mjBasicInfoDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public MyPage<MjBasicInfo> queryByPage(CommonBasicDTO commonBasicDTO) {
        PageHelper.startPage(commonBasicDTO.getPageNum(), commonBasicDTO.getPageSize());
        LambdaQueryWrapper<MjBasicInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (commonBasicDTO.getType() != null && commonBasicDTO.getType() !=15) {
            queryWrapper.eq(MjBasicInfo::getType, commonBasicDTO.getType());
        }
        if (commonBasicDTO.getType() != null && commonBasicDTO.getType() ==15) {
            queryWrapper.in(MjBasicInfo::getType, Arrays.asList(1,2,3,4,5));
        }
        queryWrapper.orderByAsc(MjBasicInfo::getSort);
        List<MjBasicInfo> page = this.list(queryWrapper);
        PageInfo<MjBasicInfo> info = new PageInfo<>(page);
        return PageUtil.PageInfo2MyPage(info);

    }

    /**
     * 新增数据
     *
     * @param mjBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MjBasicInfo insert(MjBasicInfo mjBasicInfo) {
        if(mjBasicInfo.getType()==1)
        {
        mjBasicInfo.setTypeName("天气预报");
        }else if(mjBasicInfo.getType()==2)
        {
            mjBasicInfo.setTypeName("商业气象服务");
        }
        else if(mjBasicInfo.getType()==3)
        {
            mjBasicInfo.setTypeName("气象研究");
        }
        else if(mjBasicInfo.getType()==4)
        {
            mjBasicInfo.setTypeName("关于我们");
        }
        else if(mjBasicInfo.getType()==5)
        {
            mjBasicInfo.setTypeName("联系我们");
        }
        else if(mjBasicInfo.getType()==7)
        {
            mjBasicInfo.setTypeName("行业领军客户的选择");
        }
        this.mjBasicInfoDao.insert(mjBasicInfo);
        return mjBasicInfo;
    }

    /**
     * 修改数据
     *
     * @param mjBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MjBasicInfo update(MjBasicInfo mjBasicInfo) {
        LambdaQueryWrapper<MjBasicInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjBasicInfo::getId,mjBasicInfo.getId());
        if(mjBasicInfo.getType()==1)
        {
            mjBasicInfo.setTypeName("天气预报");
        }else if(mjBasicInfo.getType()==2)
        {
            mjBasicInfo.setTypeName("商业气象服务");
        }
        else if(mjBasicInfo.getType()==3)
        {
            mjBasicInfo.setTypeName("气象研究");
        }
        else if(mjBasicInfo.getType()==4)
        {
            mjBasicInfo.setTypeName("关于我们");
        }
        else if(mjBasicInfo.getType()==5)
        {
            mjBasicInfo.setTypeName("联系我们");
        }
        else if(mjBasicInfo.getType()==7)
        {
            mjBasicInfo.setTypeName("行业领军客户的选择");
        }
        this.mjBasicInfoDao.update(mjBasicInfo,queryWrapper);
        return this.queryById(mjBasicInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjBasicInfoDao.deleteById(id) > 0;
    }
}
